import request from '../../utils/request.js'
const state = {
	categoryList:[],
	textList:[],
	appList:[],
	swiperList:[]
}
const mutations = {
	SET_CATEGORYLIST(state,categoryList){
		state.categoryList=categoryList
		// console.log(state);
	},
	SET_TEXTLIST(state,textList){
		state.textList=textList
	},
	SET_APPLIST(state,appList){
		state.appList=appList
	},
	SET_SWIPERLIST(state,swiperList){
		state.swiperList=swiperList
	},
	// 加载更多
	GET_MOREAPPLIST(state){
		state.appList.push(...state.appList)
	}
}
const actions = {
	async getFindData({commit}){
		const result = await request('/getFindData')
		let categoryList=result[0].data.map(item=>{
			return{
				imageUrl:item.icon.medium_url,
				name:item.label
			}
		})
		commit('SET_CATEGORYLIST',categoryList)
		
		let textList=result[1].data.map(item=>{
			return{
				imgUrl:item.icon.medium_url||"",
				name:item.label||""
			}
		})
		commit('SET_TEXTLIST',textList)
		
		let appList=result.slice(2,11).map(item=>{
			if(item.data[0].author){
				return {
					title:item.label||"",
					msgList:item.data.map(item1=>{
						return{
							author:{
								avatar:item1.author?.avatar,
								id:item1.author?.id,
								name:item1.author?.name
							},
							imgUrl:item1.app?.banner.url,
							title:item1.app?.title,
							contents:item1.contents?.text,
							score:item1.score,
							id:item1.id
						}
					}),
					type:1
				}
			}else if(!item.data[0].icon){
				return{
					type:2,
					title:item.label||"",
					bannerList:item.data.map(item1=>{
						return {
							id:item1.id||"",
							imgUrl:item1.banner.medium_url||"",
							title:item1.title||""
						}
					}),
				}
			}else{
				return {
					title:item.label||"",
					bannerList:item.data.map(item1=>{
						return {
							id:item1.id||"",
							imgUrl:item1.icon.url||"",
							title:item1.title||""
						}
					}),
					type:0
				}
			}
			
		})
		commit('SET_APPLIST',appList)
		
		let swiperList=result[11].data.map(item=>{
			return item.banner.url
		})
		commit('SET_SWIPERLIST',swiperList)
	}
}
const getters = {}

export default ({
	state,
	mutations,
	actions,
	getters, 
})
